#include "bsp_tim.h"
#include "iwdg.h"

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
    // if(htim == &RGB_TIM)
    // {
    //     if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
    //     {
    //        printf("test stop\n");
    //     }
    // }

//    HAL_TIM_PWM_Stop_DMA(&htim15,TIM_CHANNEL_1);
}

/**
  * @brief  1us delay for GD32F427 at 180M
  * @param  void
  * @retval void
  */
void bsp_delay_us(uint32_t us)
{
    volatile uint32_t i = 0;
    while(i < 18 * us)
    {
        i++;
    }
}
/**
  * @brief  1ms delay for GD32F427 at 180M
  * @param  void
  * @retval void
  */
void bsp_delay_ms(uint32_t ms)
{
    volatile uint32_t k = 0;
    for(k=0;k<ms;k++)
    {
        HAL_IWDG_Refresh(&hiwdg);
        bsp_delay_us(1000);
    }
}

/**
  * @brief  
  * @param  void
  * @retval void
  */
 void pwm_set_ccr(void *tim_p, uint32_t channel, uint32_t value_t)
 {
     TIM_HandleTypeDef tim = *(TIM_HandleTypeDef*)(tim_p);
     switch(channel)
     {
         case TIM_CHANNEL_1:
             tim.Instance->CCR1 = value_t;
         break;
 
         case TIM_CHANNEL_2:
             tim.Instance->CCR2 = value_t;
         break;
         
         case TIM_CHANNEL_3:
             tim.Instance->CCR3 = value_t;
         break;
 
         case TIM_CHANNEL_4:
             tim.Instance->CCR4 = value_t;
         break;
 
         default:
             /*  do nothing  */
         break;
     }
 }